<?php

namespace App\V3\V3_2_6\Services;

use App\V3\V3_2_6\Models\CustomTagModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use mysql_xdevapi\Exception;

class CustomTagService
{

    static public function addCustomTag($data)
    {
        if($data['user_id'] == $data['main_id']){
            $data['label_name']=$data['label_name'];
            $data['create_time']=time();
            $data['created_at']=time();
            DB::beginTransaction();
            try {
                $id=CustomTagModel::addCustomTag($data);
                DB::commit();
                return ['code'=>0,'msg'=>'success','data'=>['label_id'=>$id]];
            }catch (\Exception $e){
                DB::rollBack();
                Log::debug('自定义标签新建失败',var_export($e->getMessage(),true));
                return ['code'=>1,'msg'=>'自定义标签，新建失败！','data'=>''];
            }
        }else{
            return ['code'=>1,'msg'=>'请用管理员账号，创建标签！','data'=>''];
        }

    }

    static public function getCustomTagList($condition,$data)
    {
        return CustomTagModel::getCustomTagList($condition,$data);
    }

    static public function updCustomTag($condition,$updData)
    {
        if($condition['user_id'] == $condition['main_id']){
            $updData['updated_at']=time();
            DB::beginTransaction();
            try {
                CustomTagModel::updCustomTag($condition,$updData);
                DB::commit();
                return ['code'=>0,'msg'=>'success','data'=>''];
            }catch (\Exception $e){
                DB::rollBack();
                Log::debug('自定义标签新建失败',var_export($e->getMessage(),true));
                return ['code'=>1,'msg'=>'自定义标签，新建失败！','data'=>''];
            }
        }else{
            return ['code'=>1,'msg'=>'请用管理员账号，创建标签！','data'=>''];
        }
    }

    static public function delCustomTag($condition)
    {
        try {
            CustomTagModel::delCustomTag($condition);
            return ['code'=>0,'msg'=>'success','data'=>''];
        }catch (\Exception $e){
            LOG::debug('自定义标签删除失败',var_export($e->getMessage(),true));
            return ['code'=>1,'msg'=>'删除失败！','data'=>''];
        }
    }

    static public function getCustomTagInfo($condition)
    {
        return CustomTagModel::getCustomTagInfo($condition);
    }
}
